package com.kwan.xframe.common.widget.dialog.base;

import android.content.DialogInterface;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Gravity;
import android.view.View;

import com.kwan.xframe.common.widget.dialog.listener.OnBindViewListener;
import com.kwan.xframe.common.widget.dialog.listener.OnViewClickListener;

import java.io.Serializable;

/**
 * 数据保存封装的容器类
 **/
public class TController implements Parcelable, Serializable {

	private FragmentManager fragmentManager;
	private int layoutRes;
	private int height;
	private int width;
	private float dimAmount;
	private int gravity;
	private String tag;
	private int[] ids;
	private boolean isCancelableOutside;
	private OnViewClickListener onViewClickListener;
	private OnBindViewListener onBindViewListener;

	private boolean cancelable;//弹窗是否可以取消
	private View dialogView;
	private DialogInterface.OnDismissListener onDismissListener;

	//////////////////////////////////////////Parcelable持久化//////////////////////////////////////////////////////
	public TController() {
	}

	protected TController(Parcel in) {
		layoutRes = in.readInt();
		height = in.readInt();
		width = in.readInt();
		dimAmount = in.readFloat();
		gravity = in.readInt();
		tag = in.readString();
		ids = in.createIntArray();
		isCancelableOutside = in.readByte() != 0;
		cancelable = in.readByte() != 0;
	}

	public static final Creator<TController> CREATOR = new Creator<TController>() {
		@Override
		public TController createFromParcel(Parcel in) {
			return new TController(in);
		}

		@Override
		public TController[] newArray(int size) {
			return new TController[size];
		}
	};

	//内容描述接口,不用管
	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(layoutRes);
		dest.writeInt(height);
		dest.writeInt(width);
		dest.writeFloat(dimAmount);
		dest.writeInt(gravity);
		dest.writeString(tag);
		dest.writeIntArray(ids);
		dest.writeByte((byte) (isCancelableOutside ? 1 : 0));
		dest.writeByte((byte) (cancelable ? 1 : 0));
	}

	public FragmentManager getFragmentManager() {
		return fragmentManager;
	}

	public int getLayoutRes() {
		return layoutRes;
	}

	public void setLayoutRes(int layoutRes) {
		this.layoutRes = layoutRes;
	}

	public int getHeight() {
		return height;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int mWidth) {
		this.width = mWidth;
	}

	public float getDimAmount() {
		return dimAmount;
	}

	public int getGravity() {
		return gravity;
	}

	public String getTag() {
		return tag;
	}

	public int[] getIds() {
		return ids;
	}

	public boolean isCancelableOutside() {
		return isCancelableOutside;
	}

	public OnViewClickListener getOnViewClickListener() {
		return onViewClickListener;
	}

	public boolean isCancelable() {
		return cancelable;
	}

	public OnBindViewListener getOnBindViewListener() {
		return onBindViewListener;
	}

	public DialogInterface.OnDismissListener getOnDismissListener() {
		return onDismissListener;
	}

	public View getDialogView() {
		return dialogView;
	}


	public static class TParams {

		public FragmentManager mFragmentManager;
		public int mLayoutRes;
		public int mWidth;
		public int mHeight;
		public float mDimAmount = 0.2f;
		public int mGravity = Gravity.CENTER;
		public String mTag = "XDialog";
		public int[] ids;
		public boolean mIsCancelableOutside = true;
		public OnViewClickListener mOnViewClickListener;
		public OnBindViewListener bindViewListener;
		public int orientation = LinearLayoutManager.VERTICAL;//默认RecyclerView的列表方向为垂直方向
		public boolean mCancelable = true;//弹窗是否可以取消
		public View mDialogView;//直接使用传入进来的View,而不需要通过解析Xml
		public DialogInterface.OnDismissListener mOnDismissListener;

		public void apply(TController tController) {
			tController.fragmentManager = mFragmentManager;
			if (mLayoutRes > 0) {
				tController.layoutRes = mLayoutRes;
			}
			if (mDialogView != null) {
				tController.dialogView = mDialogView;
			}
			if (mWidth > 0) {
				tController.width = mWidth;
			}
			if (mHeight > 0) {
				tController.height = mHeight;
			}
			tController.dimAmount = mDimAmount;
			tController.gravity = mGravity;
			tController.tag = mTag;
			if (ids != null) {
				tController.ids = ids;
			}
			tController.isCancelableOutside = mIsCancelableOutside;
			tController.onViewClickListener = mOnViewClickListener;
			tController.onBindViewListener = bindViewListener;
			tController.onDismissListener = mOnDismissListener;

			if (tController.getLayoutRes() <= 0 && tController.getDialogView() == null) {
				throw new IllegalArgumentException("请先调用setLayoutRes()方法设置弹窗所需的xml布局!");
			}

			//弹窗是否可以取消
			tController.cancelable = mCancelable;
		}
	}
}
